"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 非所有者系统管理员修改存储过程所属模式
Create At   : 2022/05/10
Owner       : @zou_jialiang050
Description :
    1.创建用户
    2.创建schema
    3.新建用户创建procedure
    4.系统管理员修改存储过程所在模式
    5.结合系统表pg_proc,pg_namespace查询存储过程所属schema
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.返回修改后的schema
    6.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PROCEDURE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user = 'u_alter_procedure_0011'
        self.table = 't_alter_procedure_0011'
        self.procedure = 'p_alter_procedure_0011'
        self.schema = 's_alter_procedure_0011'

    def test_alter_procedure(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user} cascade;
            create user {self.user} with password '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:创建schema  expect:成功'
        self.logger.info(step)
        create_schema = self.primary_sh.execut_db_sql(f'''
            drop schema if exists {self.schema};
            create schema {self.schema};''')
        self.logger.info(create_schema)
        self.assertIn(self.Con.CREATE_SCHEMA_SUCCESS_MSG, create_schema,
                      '执行失败' + step)

        step = 'step3:新建用户创建procedure expect:成功'
        self.logger.info(step)
        create_pro = self.primary_sh.execut_db_sql(
            f'''create or replace procedure {self.procedure}()
            is
            var1 int:= 1;
            var2 int:= 2;
            begin
            drop table if exists {self.table};
            create table {self.table}(c_int int);
            insert into {self.table} values(var1),(var2);
            end;''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(create_pro)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_pro,
                      '执行失败' + step)

        step = 'step4:系统管理员修改存储过程所在模式 expect:成功'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''set current_schema = {self.user};
            alter procedure {self.procedure}() set schema {self.schema};''')
        self.logger.info(alter_procedure)
        self.assertIn(self.Con.ALTER_FUNCTION_SUCCESS_MSG, alter_procedure,
                      '执行失败' + step)

        step = 'step5:结合系统表pg_proc,pg_namespace查询存储过程所属schema ' \
               'expect:返回修改后的schema'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''select nspname from pg_namespace where oid =
            (select pronamespace from pg_proc where proname = 
            '{self.procedure}');''')
        self.logger.info(alter_procedure)
        self.assertIn(self.schema, alter_procedure, '执行失败' + step)

    def tearDown(self):
        step = 'step6:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop schema {self.schema} cascade;
            drop user {self.user};''')
        self.logger.info(clean_environment)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, clean_environment,
                      '执行失败' + step)
        self.assertIn(self.Con.DROP_SCHEMA_SUCCESS_MSG, clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
